home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / dhcp / dhcp-relay.frm.z / dhcp-relay.frm
Encoding:
Text File  |  2002-06-12  |  10.5 KB  |  389 lines

  1. #!/usr/bin/perl5
  2. #
  3. # dhcp-relay.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: dhcp-relay.frm,v 1.32 1997/11/17 19:04:33 shotes Exp $
  21.  
  22. # To determine that dhcp_relay is currently enabled, the uncommented line
  23. # must exist in inetd.conf and chkconfig proclaim_relayagent must be on.
  24. #
  25. # To enable, both the above are set, and a sig HUP is sent.
  26. #
  27. # To disable, the line is commented and chkconfig p... is set off and sig 
  28. # HUP sent. 
  29.  
  30. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  31. require "/usr/OnRamp/lib/OnRamp.pm";
  32. require "/usr/OnRamp/lib/java.pm";
  33.  
  34. $title = "DHCP Relay Agent";
  35. $help_page = "dhcp-relay-help.html";
  36.  
  37. $js = 
  38. "type = \"server\";
  39. $js_account_main
  40. $js_help
  41. $js_error_box
  42. $js_ip
  43. $js_hostname
  44. function testAdd(form)  {
  45.     Ctrl = form.add_list;
  46.     if (Ctrl.value == \"\") {
  47.         errorBox (Ctrl, \"To add a new server, first enter the \"
  48.             + \"hostname \\nor IP address, then click \"
  49.             + \"the \\\"Add New Server\\\" \\nbutton.\");
  50.         return (false);
  51.     }
  52.     if (!testThisHost(form.add_list)) return (false);
  53.     return (true);
  54. }
  55. function testThisHost(Ctrl) {
  56.     start = 0;  
  57.     while (Ctrl.value.charAt(start) == ' ') { start++; }
  58.     c = Ctrl.value.charAt(start);
  59.     if (! checkInt_c(c, 10)) { // this is not an IP address
  60.         if (!testHostname(Ctrl, Ctrl.value, \"hostname\", 0)) return (false);
  61.         return (true);
  62.     } else {
  63.         if (!testIPaddress(Ctrl.value, false)) {
  64.             errorBox(Ctrl, \"Invalid IP address.\");
  65.             return (false);
  66.         }
  67.     } 
  68.     return (true);
  69. }";
  70.  
  71.  
  72. $query = new CGI;
  73. print $query->header;
  74.  
  75. $help = $document_root . $ENV{"SCRIPT_NAME"};
  76. $help =~ s/cgi$/hlp/;
  77. exec $help if ($query->param('help') eq "Help");
  78.  
  79. &js_title_block($title,$js);
  80.  
  81. $def_config_dir = "/var/dhcp/config";
  82. $rly_off = system ("/etc/chkconfig", "proclaim_relayagent");
  83. $srv_off = system ("/etc/chkconfig", "proclaim_server");
  84. $def_rstate = "No";
  85. if ($rly_off == 0) {
  86.     $def_rstate = "Yes";
  87. }
  88. $inetd_cf = "/etc/inetd.conf";
  89. $dummy = "/etc/inetd.conf.tmp";
  90.  
  91. # Only look at the option file name in /etc/inetd.conf if the server is
  92. # enabled, also need to process it then.
  93. if ($rly_off == 0) {    # chkconfig proclaim_relayagent is on
  94.     open(INETD, $inetd_cf) || &error("Cannot open $inetd_cf");
  95.     while (<INETD>) {
  96.     if (/^#/) {
  97.         next;
  98.     }
  99.     # bootp dgram udp wait root /usr/etc/dhcp_relay dhcp_relay -o <file>
  100.     if (/^bootp/) {
  101.         chop;
  102.         @entlist = split(/\s+/);
  103.         if (($entlist[6] eq "dhcp_relay") && ($entlist[7] eq "-o")) {
  104.         $osf = $entlist[8];
  105.         last;    # Break out of the while loop
  106.         }
  107.     }
  108.     }
  109.     close(INETD);
  110. }
  111.  
  112. if (($rly_off == 0) && $osf) {
  113.     open(OPTIONS, $osf) || &error("Cannot open $osf");
  114.     while(<OPTIONS>) {
  115.     if (/^#/) {
  116.         next;
  117.     }
  118.     chop;
  119.     @optlist = split(/\s+/);
  120.     $i = 0;
  121.     while ($optlist[$i]) {
  122.         if ($optlist[$i] eq "-c") {
  123.         $def_config_dir = $optlist[$i+1];
  124.         $i += 2;
  125.         next;
  126.         }
  127.         else {
  128.         print $err_start,
  129.               "Unknown Option $optlist[$i] in the <b>$osf</b> file.";
  130.         }
  131.         $i++;
  132.     }
  133.     }
  134.     close(OPTIONS);
  135. }
  136. else {
  137.     $osf = "/etc/config/dhcp_relay.options";
  138. }
  139.  
  140. $srvlist_file = $def_config_dir."\/dhcp_relay.servers";
  141.  
  142. open(SRVLST, $srvlist_file) || &error("Cannot open $srvlist_file");
  143. $i = 0;
  144. while (<SRVLST>) {
  145.     if (/^#/) {
  146.     next;
  147.     }
  148.     chop;
  149.     $_ =~ s/^\s+//;
  150.     $_ =~ s/\s+$//;
  151.     $server_list[$i] = $_;
  152.     $i++;
  153. }
  154. close(SRVLST);
  155.  
  156. undef @new_list;
  157.  
  158. $addServer = 0;
  159. $delServer = 0;
  160.  
  161. if ($query->param) {
  162.     if ($query->param('add') eq 'Add New Server') {
  163.         $addList = $query->param('add_list');
  164.         &formValid_add;
  165.         $message = qq|Click "Ok" to save changes.|;
  166.         $query->param('add_list','');
  167.         &generic($addList,1);
  168.         exit 0;
  169.     }
  170.  
  171.     elsif ($query->param('delete') eq 'Delete Existing Server') {
  172.         $delList = $query->param('chosen');
  173.         &error("To delete an existing server, first select one from
  174.         the list, then click the delete button.") if !$delList;
  175.         $delList =~ /([\w.-]+)/;  
  176.         $delList = $1;
  177.         $message = qq|Click "Ok" to save changes.|;
  178.         &generic($delList,2);
  179.         exit 0;
  180.     }
  181.  
  182.     elsif ($query->param('doit') eq 'Ok') {
  183.         if ($query->param('addName')) { 
  184.             push(@server_list,$query->param('addName'));
  185.             &saveList;
  186.             $message = "Server added."; 
  187.         }
  188.         if ($query->param('delName')) {
  189.             $delName = $query->param('delName');
  190.             undef @temp_list;
  191.             foreach $arg (@server_list) {
  192.                 if ($arg ne $delName) { push(@temp_list,$arg); }
  193.             }
  194.             @server_list = @temp_list;
  195.             &saveList; 
  196.             $message = "Server deleted."; 
  197.         }
  198.  
  199.         $new_rstate = $query->param(rstate);
  200.  
  201.         if ($new_rstate ne $def_rstate) {    # State Changed
  202.             # Need to make sure that DHCP Server is disabled
  203.             if ($new_rstate eq "Yes") {
  204.                 # chkconfig on, the system WILL ship with the inetd.conf entry
  205.                 # for dhcp_bootp enabled
  206.                 if ($srv_off == 0) {
  207.                     &error("You Need to Disable the DHCP Server before you
  208.                     can enable the DHCP Relay Agent.");
  209.                 }
  210.                 system ("/etc/chkconfig", "autoconfig_ipaddress", "off");
  211.                 system ("/etc/chkconfig", "proclaim_relayagent", "on");
  212.                 &checkForInetd;
  213.                 $message .= "  DHCP relay agent enabled.";
  214.             }
  215.             else {
  216.                 # chkconfig off
  217.                 system ("/etc/chkconfig", "proclaim_relayagent", "off");
  218.                 &commentInetd;
  219.                 $message .= "  DHCP relay agent disabled.";
  220.             }
  221.             system("/etc/killall", "-HUP", "inetd");
  222.         }
  223.         if (!$message) { $message = "No changes made."; }
  224.     }
  225.     else {
  226.         $message = "Use buttons to submit form.";
  227.         &generic;
  228.         exit 0;
  229.     }
  230. }
  231.  
  232. sub commentInetd {
  233.     open(IN,"< $inetd_cf");
  234.     open(OUT,"> $dummy");
  235.     while(<IN>) {
  236.     @items = split(/\s+/);
  237.     if ($items[0] eq "bootp" && $items[6] eq "dhcp_relay") {
  238.         print OUT "# $_";
  239.     } else { print OUT $_; }
  240.     }
  241.     close(IN);
  242.     close(OUT);
  243.     rename($dummy,$inetd_cf);
  244. }    
  245.  
  246. sub checkForInetd {
  247.     $line = "bootp\tdgram\tudp\twait\troot\t/usr/etc/dhcp_relay ".
  248.         "dhcp_relay -o /etc/config/dhcp_relay.options";
  249.     $rename = 0; $found = 0;
  250.     open(IN,"< $inetd_cf");
  251.     open(OUT,"> $dummy");
  252.     while(<IN>) {
  253.     @items = split(/\s+/);
  254.     if ($items[6] eq "dhcp_relay" || $items[7] eq "dhcp_relay") {
  255.         $found = 1; 
  256.         if (substr($items[0],0,1) eq "#") {
  257.         print OUT "$line\n";
  258.         $rename = 1;
  259.         }
  260.     } else { print OUT $_; }
  261.     }
  262.     if (!$found) { print OUT "$line\n"; $rename = 1; }
  263.     close(IN);
  264.     close(OUT);
  265.     if ($rename) { rename($dummy,$inetd_cf); }
  266. }
  267.  
  268. sub formValid_add {
  269.     &error("Server name or address required.") if !$addList;
  270.     if ($addList =~ /[^0-9.]/) {    # assume this is not an IP number 
  271.     &error("Please input IP address in dot notation.") if $addList =~ /0x/;
  272.     &error("Invalid hostname: $addList.") if &check_hostname($addList);
  273.     ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($addList);
  274.     &error("Host cannot be resolved.") if !@addrs;
  275.     } else {                # IP num
  276.     &error("Invalid IP address.") if &check_ipaddr($addList);    
  277.     }
  278. }
  279.  
  280. sub saveList {
  281.     open(SRVLST, ">$srvlist_file") ||
  282.     &error("Cannot open file $srvlist_file for writing.");
  283.     foreach $arg (@server_list) {
  284.     print SRVLST "$arg\n";
  285.     }
  286.     close(SRVLST);
  287. }    
  288.  
  289. &generic;
  290.  
  291. sub generic {
  292.     &header_block($title);
  293.  
  294.     print "<i>$message</i>";
  295.  
  296.     print "<form method=post name=AccountForm onSubmit=\"return runSubmit()\">\n";
  297.  
  298.     print "<center><table cellpadding=5 width=450>\n";
  299.  
  300.     print "<tr><th align=left>Enable DHCP relay agent:</th><th align=left>\n",
  301.       $query->radio_group(-name=>'rstate',
  302.               -values=>['Yes','No'],
  303.               -default=>$def_rstate),
  304.       "</th></tr>";
  305.  
  306.     print '<tr><th align=left><input type=submit name="add" ',
  307.           'value="Add New Server" onClick="return markAdd()"></th><td>',
  308.           $query->textfield(-name=>'add_list', -size=>20),'</td></tr>';
  309.  
  310.     if ($_[1] == 1) {
  311.     print "<input type=hidden name=addName value=$_[0]>"; 
  312.     @locList = @server_list;
  313.     push(@locList,$_[0]);
  314.     } elsif ($_[1] == 2) {
  315.     print "<input type=hidden name=delName value=$_[0]>"; 
  316.     undef @locList;
  317.     foreach $arg (@server_list) {
  318.         if ($arg ne $_[0]) { push(@locList,$arg); }
  319.     }
  320.     } else { @locList = @server_list; }
  321.  
  322.     print '<tr><th align=left valign=top><input type=submit name="delete" ',
  323.           'value="Delete Existing Server" onClick="return markDelete()">',
  324.           '</th><td>',&choice_list(*locList,"chosen",20),'</td></tr>';
  325.     
  326.     print "</table><br>";
  327.  
  328.     print &js_buttons('doit','Ok','onClick="markOther()"',
  329.         'onClick="markOther()"',
  330.         "onClick=\"do_help('$help_page'); return (false)\"");
  331.  
  332.     print $query->endform;
  333. }
  334.  
  335. sub oldgeneric {
  336.     &header_block($title);
  337.  
  338.     print "<i>$message</i>";
  339.  
  340.     print $query->startform;
  341.  
  342.     print "<center><table cellpadding=5 width=450>\n";
  343.  
  344.     print "<tr><th align=left>Enable DHCP relay agent:</th><th align=left>\n",
  345.       $query->radio_group(-name=>'rstate',
  346.               -values=>['Yes','No'],
  347.               -default=>$def_rstate),
  348.       "</th></tr>";
  349.  
  350.     print "<tr><th align=left>Directory for server configuration files:</th>",
  351.         "<th align=left>\n",
  352.       $query->textfield(-name=>'conf_dir',
  353.                 -default=>$def_config_dir,
  354.                 -size=>20, -maxlength=>256),
  355.       "</th></tr>";
  356.  
  357.     print "<tr><th align=left>List of DHCP Servers:</th><th align=left>\n",
  358.       $query->scrolling_list(-name=>'slist', -values=>\@server_list,
  359.                      -size=>6, -multiple=>'true'),
  360.       "</th></tr>";
  361.  
  362.     print "<tr><th align=left>Delete Selected Servers from the List:</th><th align=left>\n",
  363.       $query->radio_group(-name=>'delstate',
  364.                   -values=>['Yes','No'],
  365.                   -default=>'No'),
  366.       "</th></tr>";
  367.  
  368.     print "<tr><th align=left>List of new DHCP Servers to Add:</th>",
  369.       "<th align=top>",
  370.       $query->textfield(-name=>'add_list',
  371.                 -size=>20, -maxlength=>256),
  372.       "</th></tr>";
  373.  
  374.     print "</table></center><br>";
  375.  
  376.     &button_table($query,"doit", "Ok", "help", "Help");
  377.     print $query->endform;
  378.  
  379. }
  380.  
  381.  
  382. sub
  383. error
  384. {
  385.     &error_block($_[0]);
  386.     &generic;
  387.     exit 0;
  388. }
  389.